﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace IngredSubTool
{
    /// <summary>
    /// Interaction logic for ConversionCalcView.xaml
    /// </summary>
    public partial class ConversionCalcView : UserControl, IAppPage
    {
       // int back_flag = MainWindow.session.previousPage;
        int calcViewFlag = 0;
        private int initIngUnit = 0;
        private int initSubUnit = 0;
        public ConversionCalcView()
        {
            InitializeComponent();
            Menu.Content = new SideMenu();
            UnitChangeIngredient.ItemsSource = DataLoader.Units;
            UnitChangeIngredient.DisplayMemberPath = "UnitName";
            UnitChangeIngredient.SelectedValuePath = "UnitValue";
            UnitChangeIngredient.SelectedIndex = 0;

            UnitChangeSubstitute.ItemsSource = DataLoader.Units;
            UnitChangeSubstitute.DisplayMemberPath = "UnitName";
            UnitChangeSubstitute.SelectedValuePath = "UnitValue";
            UnitChangeSubstitute.SelectedIndex = 0;

            if (MainWindow.session.selectedSub != null)
            {
                Substitution sub = MainWindow.session.selectedSub;
                OriginalIngredient.Text = sub.Original;
                ReplacementIngredient.Text = sub.Replacement;
                NotesOnSubstitution.Text = sub.Description;
                IngAmountVal.Text = sub.Ratios.Item1.ToString();
                SubAmountVal.Text = sub.Ratios.Item2.ToString();
                for (int i = 0; i < UnitChangeIngredient.Items.Count; i++)
                {
                    UnitsOfMeasure temp = (UnitsOfMeasure)UnitChangeIngredient.Items[i];
                    if (sub.Ratios.Item3 == temp.UnitName)
                    {
                        UnitChangeIngredient.SelectedIndex = i;
                        initIngUnit = int.Parse(UnitChangeIngredient.SelectedValue.ToString());
                    }
                    if (sub.Ratios.Item4 == temp.UnitName)
                    {
                        UnitChangeSubstitute.SelectedIndex = i;
                        initSubUnit = int.Parse(UnitChangeSubstitute.SelectedValue.ToString());
                    }
                    // case where "Each"
                    if (sub.Ratios.Item3 == "Each")
                    {
                        UnitChangeIngredient.SelectedIndex = -1;
                        UnitChangeIngredient.IsEditable = true;
                        UnitChangeIngredient.Text = "Each";
                        UnitChangeIngredient.IsEnabled = false;
                    }
                    if (sub.Ratios.Item4 == "Each")
                    {
                        UnitChangeSubstitute.SelectedIndex = -1;
                        UnitChangeSubstitute.IsEditable = true;
                        UnitChangeSubstitute.Text = "Each";
                        UnitChangeSubstitute.IsEnabled = false;
                    }
                }
             //   back_flag = 1;
            }
           // else { back_flag = 0; }
            calcViewFlag = 1;
        }

        public void LoadSession(Session session)
        {
            throw new ApplicationException("Session access not yet implemented.");
        }

        private void Back(object sender, RoutedEventArgs e)
        {
            /*
            if (MainWindow.session.previousPage == MainWindow.session.homePage)
            {
                PageSwitcher.Switch(new SearchView());
            }
            if (MainWindow.session.previousPage == MainWindow.session.searchResultPage)
            {
                PageSwitcher.Switch(new SearchResultsView());
            }
            if (MainWindow.session.previousPage == MainWindow.session.mySubPage)
            {
                PageSwitcher.Switch(new MySubstitutionView());
            }
            if (MainWindow.session.previousPage == MainWindow.session.bookmarkPage)
            {
                PageSwitcher.Switch(new BookmarkView());
            }
            MainWindow.session.previousPage = MainWindow.session.homePage;
             * */
            //PageSwitcher.Switch(MainWindow.session.lastPage);
            PageSwitcher.SwitchBack();
       
        }

        // Method used to clear input box of the watermark "0.0"

        private void IngAmountVal_GotFocus(object sender, RoutedEventArgs e)
        {
            IngAmountVal.Clear();
        }

        private void IngAmountVal_LostFocus(object sender, RoutedEventArgs e)
        {
            if (IngAmountVal.Text == "")
            {
                IngAmountVal.Text = "0.0";
            }
        }

        private void Calculate_Click(object sender, RoutedEventArgs e)
        {
            Substitution sub = MainWindow.session.selectedSub;
            double amount;
            double subVal;
            ErrorMessageBox.Text = "";
            // If the text entered in the box is a double and not a string
            if (double.TryParse(IngAmountVal.Text, out amount))
            {
                // Both comboboxes are "Each"
                if (!UnitChangeIngredient.IsEnabled && !UnitChangeSubstitute.IsEnabled)
                {
                    subVal = amount / (sub.Ratios.Item1 / sub.Ratios.Item2);
                    SubAmountVal.Text = subVal.ToString();
             }
            
                // Ingredient combobox is "Each" but the sub combobox is not.
                else if (!UnitChangeIngredient.IsEnabled && UnitChangeSubstitute.IsEnabled)
                {

                    subVal = amount / (sub.Ratios.Item1 / sub.Ratios.Item2);
                    subVal *= ((double)initSubUnit / double.Parse(UnitChangeSubstitute.SelectedValue.ToString()));
                    SubAmountVal.Text = subVal.ToString();
                }
                // Neither side is "Each"
                else
                {
                    subVal = amount * (double.Parse(UnitChangeIngredient.SelectedValue.ToString()) / (sub.Ratios.Item1 * (double)initIngUnit));
                    subVal *= ((sub.Ratios.Item2 * (double)initSubUnit) / double.Parse(UnitChangeSubstitute.SelectedValue.ToString()));
                    SubAmountVal.Text = subVal.ToString();
                }

            }

            // To catch errors if user gives invalid input
            else
            {
                ErrorMessageBox.Text = "Error: Invalid Input, You May Only Enter Numbers";
                IngAmountVal.Text = "0.0";
                SubAmountVal.Text = "0.0";
            }
        }

        private void UnitChangeIngredient_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (calcViewFlag == 1)
            {
                Calculate_Click(sender, e);
            }
        }

        private void UnitChangeSubstitute_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (calcViewFlag == 1)
            {
                Calculate_Click(sender, e);
            }
        }

        private void IngAmountVal_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Calculate_Click(sender, e);
            }
        }
    }
}
